package com.bawei.library_log.logger

import android.content.Context
import android.util.Log
import com.bawei.library_log.ILogger
import com.bawei.library_log.LoggerLevel
import java.io.File
import java.io.FileWriter
import java.text.SimpleDateFormat
import java.util.*

/**
 * @Author : 
 * @Time : On 2023/6/20 10:24
 * @Description : FileLogger
 */
class FileLogger:ILogger {
    private var isDebug = true //是否Debug
    private var TAG = "" //默认TAG
    private var level = LoggerLevel.Debug // 默认日志等级
    private var url = "log" //Log存储位置，可以是本地路径 可以是网络路径 可以是Email地址
    private var context:Context? = null //上下文 目的是获得内部存储的路径
    private var loggerFormat = "(%s --->%s）" //格式化
    fun getFile():File{
        context.let {
            val filesDir = it?.filesDir //data/data/files
            return File(filesDir, url)
        }
    }
    fun getTime():String{
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
        val date = Date()
        return simpleDateFormat.format(date)
    }



    override fun d(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.Debug.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)


            FileWriter(getFile(),true).apply {//true代表追加数据
                write("$time D/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun v(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.Verbose.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)
            FileWriter(getFile(),true).apply {
                write("$time V/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun i(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.Info.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)
            FileWriter(getFile(),true).apply {
                write("$time I/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun w(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.Warn.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)
            FileWriter(getFile(),true).apply {
                write("$time W/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun e(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.Error.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)
            FileWriter(getFile(),true).apply {
                write("$time E/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun setDebug(isDeug: Boolean) {
        this.isDebug = isDebug
    }

    override fun setLogTAG(TAG: String) {
        this.TAG = TAG
    }

    override fun setLoggerLevel(level: LoggerLevel) {
        this.level = level
    }

    override fun setSaveUrl(url: String) {
        this.url = url
    }

    //上下文
    override fun setContext(context: Context?) {
        this.context = context
    }
}